<% content_for :page_title do %>
  <%= plural_resource_name(Spree::StockTransfer) %>
<% end %>

<% content_for :page_actions do %>
  <%= button_link_to Spree.t(:new_stock_transfer), new_admin_stock_transfer_path, { icon: 'add.svg', class: 'btn-success' } %>
<% end if can? :create, Spree::StockTransfer %>

<% content_for :table_filter do %>
  <div data-hook="admin_orders_index_search">
    <%= search_form_for @q, url: admin_stock_transfers_path do |f| %>

      <div class="row mb-0">
        <div class="col-12 col-lg-4">
          <div class="form-group">
            <%= f.label :reference_cont, Spree.t(:reference_contains) %>
            <%= f.text_field :reference_cont, class: 'form-control' %>
          </div>
        </div>

        <div class="col-12 col-lg-4">
          <div class="form-group">
            <%= f.label :source_location, Spree.t(:source) %>
            <%= f.select :source_location_id_eq,
                options_from_collection_for_select(@stock_locations, :id, :name, @q.source_location_id_eq),
                { include_blank: true }, class: 'select2-clear js-filterable' %>
          </div>
        </div>

        <div class="col-12 col-lg-4">
          <div class="form-group">
            <%= f.label :destination_location, Spree.t(:destination) %>
            <%= f.select :destination_location_id_eq,
                options_from_collection_for_select(@stock_locations, :id, :name, @q.destination_location_id_eq),
                { include_blank: true }, class: 'select2-clear js-filterable' %>
          </div>
        </div>
      </div>

      <div class="form-actions" data-hook="admin_stock_transfers_index_search_buttons">
        <%= button Spree.t(:filter_results), 'search.svg' %>
      </div>
    <% end %>
  </div>
<% end %>

<% if @stock_transfers.any? %>
<div class="table-responsive">
  <table class="table" id='listing_stock_transfers' data-hook>
    <thead>
      <tr data-hook='stock_transfers_header'>
        <th><%= Spree.t(:number) %></th>
        <th><%= Spree.t(:reference) %></th>
        <th><%= Spree.t(:source) %></th>
        <th><%= Spree.t(:destination) %></th>
        <th><%= Spree.t(:created_at) %></th>
        <th class='actions'></th>
      </tr>
    </thead>
    <tbody>
      <% @stock_transfers.each do |stock_transfer| %>
        <tr id="<%= spree_dom_id stock_transfer %>" data-hook="stock_transfer_row">
          <td><%= link_to stock_transfer.number, admin_stock_transfer_path(stock_transfer) %></td>
          <td><%= stock_transfer.reference %></td>
          <td>
            <% if stock_transfer.source_location.present? %>
              <%= stock_transfer.source_location.name %>
            <% end %>
          </td>
          <td>
            <% if stock_transfer.destination_location.present? %>
              <%= stock_transfer.destination_location.name %>
            <% end %>
          </td>
          <td><%= stock_transfer.created_at.strftime('%F %T %Z') %></td>
          <td class="actions actions-1">
            <%= link_to_with_icon 'show', Spree.t(:show), admin_stock_transfer_path(stock_transfer), class: 'btn btn-outline-secondary btn-sm', no_text: true, data: {action: 'view'} %>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>
</div>
<% else %>
  <div class="alert alert-info no-objects-found">
    <%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::StockTransfer)) %>,
    <%= link_to(Spree.t(:add_one), new_admin_stock_transfer_path) if can? :create, Spree::StockTransfer %>!
  </div>
<% end %>

<%= paginate @stock_transfers, theme: 'admin-twitter-bootstrap-4' %>
